package com.jmlee.controller;

import com.jmlee.component.HostHolder;
import com.jmlee.pojo.*;
import com.jmlee.service.AOrderService;
import com.jmlee.util.JSONUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class AorderController {


    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private AOrderService aorderService;


    //==============================================订单管理===========================================================


    /**
     * 访问商品列表
     *
     * @return
     */
    @GetMapping("orderCheck")
    public String itemManager() {
        Admin admin = hostHolder.getAdmin();
        if (admin.getRoleId() == 1L || admin.getRoleId() == 3L) {
            return "orders";
        } else {
            return "shopOrders";
        }
    }

    /**
     * 查询所有的用户信息并进行分页处理
     * @return
     */
    @RequestMapping("/turnletter")
    public String turnuserorderss() {

        return "letter";
    }

    @RequestMapping("/turnorders")
    public String turnuserorders() {

        return "orders";
    }

    @RequestMapping("ordersCount")
    @ResponseBody
    public String count(AOrders aOrders, HttpServletRequest request, HttpServletResponse response) throws IOException {



        /**
         * 搜索订单
         */
        String type = request.getParameter("type");
        String keywords = request.getParameter("keywords");

//        AOrderStatus AOrderStatus = null;

        if (type != null && !type.equals("")) {
            aOrders = new AOrders();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            if ("receiverName".equals(type)) {
                aOrders.setReceiverName(keywords);
            }

        }



        int curPage = Integer.valueOf(request.getParameter("page"));
        int pageSize = Integer.valueOf(request.getParameter("limit"));

        System.out.println(aOrders.toString());

        RowBounds rowBounds = new RowBounds((curPage - 1) * pageSize, pageSize);
        PageBean<AOrders> pageBean = new PageBean<>();

        pageBean.setCode(0);
        int count = aorderService.selectShopOrderListCount(aOrders);

        pageBean.setCount(count);
        pageBean.setData(aorderService.selectShopOrderList(aOrders, rowBounds));


        String json = JSONUtil.objectToJson(pageBean);
        System.out.println("111111111111111111111:"+json);
        return json;
    }

    /**
     * 评价回复功能
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/OrderInfoSubmit")
    public String updateUserSubmit(@RequestBody AItemComments aItemComments) {
        System.err.println(aItemComments.toString());
        aorderService.insertSubmit(aItemComments);
        return "success";
    }

    @RequestMapping("shopOrdersCount")
    @ResponseBody
    public String shopcount(AOrders aOrders, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Admin admin = hostHolder.getAdmin();

        /**
         * 搜索订单
         */
        String type = request.getParameter("type");
        String keywords = request.getParameter("keywords");

        AOrderStatus AOrderStatus = null;
        if (type != null && !type.equals("")) {
            aOrders = new AOrders();
            if ("receiverName".equals(type)) {
                aOrders.setReceiverName(keywords);
            }
            if ("receiverMobile".equals(type)) {
                aOrders.setReceiverMobile(keywords);
            }
            if ("receiverMobile".equals(type)) {
                AOrderStatus.setOrderStatus(keywords);
            }

        }



        int curPage = Integer.valueOf(request.getParameter("page"));
        int pageSize = Integer.valueOf(request.getParameter("limit"));



        RowBounds rowBounds = new RowBounds((curPage - 1) * pageSize, pageSize);
        PageBean<AOrders> pageBean = new PageBean<>();

        pageBean.setCode(0);
        int count = aorderService.selectOrderListCount(aOrders,admin.getId());

        pageBean.setCount(count);
        pageBean.setData(aorderService.selectOrderList(aOrders, rowBounds,admin.getId()));


        String json = JSONUtil.objectToJson(pageBean);

        return json;
    }

    @RequestMapping("/lookOrders")
    public String lookOrderUser(Long id, Model model) {
        //根据订单id查询商品信息
        List<AOrderItems> AOrderItems = aorderService.queryOrderById(id);
        Users users=aorderService.queryUsersById(id);
        model.addAttribute("users",users);
        model.addAttribute("orderItems", AOrderItems);
        return "ordersLook";
    }
    @RequestMapping("/orderEvaluate")
    public String orderEvaluate(Long id, Model model) {
        //根据订单id查询评价信息
        AItemComments aItemComments = aorderService.queryCommentsById(id);

        Users users = aorderService.queryUsersById(id);
        model.addAttribute("users", users);
        model.addAttribute("aItemComments",aItemComments);


        return "orderEvaluate";
    }

    @RequestMapping("/orderRefund")
    public String orderRefund(Long id, Model model) {
        //根据订单id查询商品信息
        AOrders aOrders  = aorderService.queryRefundById(id);


        model.addAttribute("aOrders",aOrders);

        return "orderRefund";
    }

    /**
     * 查询用户信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/ordersUserLook")
    public String lookUser(Long id, Model model) {
        //根据订单id查询商品信息
        Users users = aorderService.queryUsersById(id);
        model.addAttribute("users", users);

        return "orderUserLook";
    }



}
